﻿//	Map_FVG C# Class Library.
//
//	infoDialog6
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SQLite;

namespace Map_Fvg
{
    public partial class infoDialog6 : Form
    {
        public infoDialog6()
        {
            InitializeComponent();

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                    cmd.CommandText = "select * from classi";

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            comboBox1.Items.Add(reader.GetString(0).ToString() + " - " + reader.GetString(1).ToString());
                        }
                    }
                    conn.Close();
                }
            }
            comboBox1.SelectedIndex = 0;
        }

        private SQLiteConnection SetConnection()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.cartellaSimb, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string s1 = textBox1.Text.Trim();
            s1 = s1.ToUpper();
            int length = s1.Length;
            if (length < 7)
            {
                MessageBox.Show("completare il codice sottoclasse");
                return;
            }

            string s2 = textBox2.Text;
            if (s2.Length == 0)
            {
                MessageBox.Show("manca specificazione proprietà");
                return;
            }

            string s3 = textBox3.Text;
            if (s3.Length == 0)
            {
                MessageBox.Show("indicare font");
                return;
            }


            Color icol = textBox4.BackColor;

            string s4 = icol.A.ToString();

            s4 = string.Concat(s4, " ", icol.R.ToString(), " ", icol.G.ToString(), " ", icol.B.ToString());

            string s5 = textBox7.Text;
            if (s3.Length == 0) s5 = " ";
           
            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                        cmd.CommandText = "select * from testo where codice_testo = '" + s1 + "'";

                        SQLiteDataReader reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                        MessageBox.Show("esiste già un altro elemento con lo stesso codice testo " + s1);
                        return;
                        }
                    conn.Close();
                }
            }

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    string ss = "insert into testo values('" + s1 + "','" + s2 + "','" + s3 + "','" + s4 + "','" + s5 + "')";

   //                                    MessageBox.Show(ss);

                    cmd.CommandText = ss;
                    cmd.ExecuteNonQuery();

                }
                conn.Close();
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FontDialog fontDialog1 = new FontDialog();
            if (fontDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = fontDialog1.Font.ToString();
                textBox5.Text = fontDialog1.Font.Size.ToString();
                textBox6.Text = fontDialog1.Font.Height.ToString();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog1 = new ColorDialog();
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox4.BackColor = colorDialog1.Color;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ss = comboBox1.Text; 
            int ipos = ss.IndexOf('-', 0);
            string codice = ss.Substring(0, ipos - 1);
            textBox1.Text = string.Concat(codice,"T");
        }


    }
}
